
//
//  TaskCenterViewController.m
//  qdSubway
//
//  Created by andi911 on 2019/5/30.
//  Copyright © 2019 andi911. All rights reserved.
//

#import "TaskCenterViewController.h"
#import "TaskCenterTableViewCell.h"
#import "TaskDetailViewController.h"
#import "SubmitFeedBackViewController.h"
#import "QDMetroClient-Swift.h"

@interface TaskCenterViewController ()<UITableViewDelegate,UITableViewDataSource,TaskCenterTableViewCellaDelegate>
//
@property (nonatomic,strong) UIButton *ongoingBtn;
//
@property (nonatomic,strong) UIButton *finishBtn;
//
@property (nonatomic,strong) UIButton *thirdBtn;
//
@property (nonatomic,strong) NSArray *showData;
//
@property (nonatomic, assign) int page;
//
@property (nonatomic, assign) int size;
//
@property (nonatomic, assign) NSInteger currentType;
//
@property (nonatomic,strong) NSMutableArray *btnArr;

@end

@implementation TaskCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.size = 7;
    self.currentType = 0;
    self.navigationItem.title = self.status>0 ?@"我的任务" :@"任务中心";
    self.view.backgroundColor = RGB(255);
    [self loadSetUpUI];
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.status>0) {
        switch (self.status) {
            case 10:
                self.ongoingBtn.selected = YES;
                break;
            case 11:
                self.finishBtn.selected = YES;
                break;
            case 12:
                self.thirdBtn.selected = YES;
                break;
            default:
                break;
        }
        [self mytaskRefreshData:self.status];
    }else{
        self.ongoingBtn.selected = YES;
        [self refrshDataType:self.currentType];
    }
    
}


- (void)refrshDataType:(NSInteger)type{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *typeStr = [[NSString alloc]initWithFormat:@"%ld",type];
    NSString *pageStr = [[NSString alloc]initWithFormat:@"%d",self.page];
    NSString *sizeStr = [[NSString alloc]initWithFormat:@"%d",self.size];
    [dict setValue:typeStr forKey:@"Type"];
    [dict setValue:pageStr forKey:@"page"];
    [dict setValue:sizeStr forKey:@"size"];
    
    __weak __typeof(self)weakSelf = self;
    [self showHudInView:self.view  hint:@"正在请求..."];
    [QDNetworkingManager requestPostUrlString:@"SupervisorTaskManagementController/TaskManagementCenter" Parameters:dict success:^(NSURLSessionDataTask * _Nonnull dataTask, NSDictionary * _Nonnull data) {
        [self hideHud];
        if (![[data valueForKey:@"respcod"] isEqualToString:@"01"]) {
            [self showHint:data[@"respmsg"]];
        }else{
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [self hideHud];
        [self showHint:@"网络请求失败"];
    }];
    
}

- (void)loadMoreListData:(NSInteger)type{
    
}


- (void)mytaskRefreshData:(int)type{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *typeStr = [[NSString alloc]initWithFormat:@"%d",type];
    NSString *pageStr = [[NSString alloc]initWithFormat:@"%d",self.page];
    NSString *sizeStr = [[NSString alloc]initWithFormat:@"%d",self.size];
    [dict setValue:typeStr forKey:@"Type"];
    [dict setValue:pageStr forKey:@"page"];
    [dict setValue:sizeStr forKey:@"size"];
    
//    __weak __typeof(self)weakSelf = self;
    [self showHudInView:self.view  hint:@"正在请求..."];
    [QDNetworkingManager requestPostUrlString:@"SupervisorTaskManagementController/TaskManagementMy" Parameters:dict success:^(NSURLSessionDataTask * _Nonnull dataTask, NSDictionary * _Nonnull data) {
        [self hideHud];
        if (![[data valueForKey:@"respcod"] isEqualToString:@"01"]) {
            [self showHint:data[@"respmsg"]];
        }else{
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [self hideHud];
        [self showHint:@"网络请求失败"];
    }];
}


- (void)loadSetUpUI{
    NSArray *titleArr = @[@"进行中",@"已结束"];
    if(self.status>0){
        titleArr = @[@"待完成",@"已完成",@"已结束"];
    }
    
    self.btnArr = [[NSMutableArray alloc]init];
    for (int i =0; i<titleArr.count; i++) {
        UIButton *btn = [[UIButton alloc]init];
        [btn setTitle:titleArr[i] forState:0];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:CLYImage(@"rectangle3Copy4") forState:UIControlStateSelected];
        [btn setBackgroundImage:CLYImage(@"rectangle3Copy7") forState:UIControlStateNormal];
        btn.width = (Screen_W- 40)/titleArr.count;
        btn.height = 40;
        btn.tag = 10+i;
        btn.center = CGPointMake(20+btn.width/2+btn.width*i, getRectNavAndStatusHight+12+btn.height/2);
        [btn addTarget:self action:@selector(tapBtn:) forControlEvents:1<<6];
        btn.selected = NO;
        [self.view addSubview:btn];
        switch (btn.tag) {
            case 10:
                self.ongoingBtn = btn;
                [self.btnArr addObject:self.ongoingBtn];
                break;
            case 11:
                self.finishBtn = btn;
                [self.btnArr addObject:self.finishBtn ];
                break;
            case 12:
                self.thirdBtn = btn;
                [self.btnArr addObject:self.thirdBtn];
                break;
            default:
                break;
        }
        
    }
    
}

#pragma mark ---- tapBtn Action ----
- (void)tapBtn:(UIButton *)sender{
    for (UIButton *btn in self.btnArr) {
        if (btn.tag == sender.tag) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
    
    [self refrshDataType:sender.tag-10];
}

#pragma mark -tableview-delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (Screen_W>375) {
        return 180;
    }
    if(Screen_W>320){
        return 210;
    }
    return 235;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TaskCenterTableViewCell *cell = [TaskCenterTableViewCell cellWithTableView:tableView];
    cell.delegate = self;
    cell.status = self.status;
    //    cell.dict = _baseArr[indexPath.row];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    TaskDetailViewController *vc = [[TaskDetailViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)tapCellBtn{
    if(self.status>0){
        SubmitFeedBackViewController *vc = [[SubmitFeedBackViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        
    }
}

#pragma mark -tableview-delegate



#pragma mark ==== lazy ====
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,getRectNavAndStatusHight+12+40,Screen_W, Screen_H-(getRectNavAndStatusHight+12+40)) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource= self;
        //除去tableview下方无数据的横线
        UILabel *label = [[UILabel alloc]init];
        _tableView.tableFooterView = label;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        __unsafe_unretained __typeof(self) weakSelf = self;

        weakSelf.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            DLog(@"下拉刷新");
            [self refrshDataType:self.currentType];
        }];
    }
    return _tableView;
}


@end
